
var isjump = 0;//判断是否跳转

var factory_id; //(参数名字不要取一样！)
var username;
var secretkey;
var countdown=60;
layui.config({
	version:'1510164124181'
}).use(['form','laydate', 'laypage', 'layer', 'element'],function(){
	form = layui.form;
	layer = layui.layer;
	$ = layui.jquery;
    laydate = layui.laydate;//日期
    laypage = layui.laypage;//分页
    element = layui.element; //元素操作
	username = $.session.get("username");
	secretkey = $.session.get("user_secretkey");
    find_gwtype();
	if(window.sessionStorage.getItem("factory_id")){
	//	console.log("所担负、=="+window.sessionStorage.getItem("factory_id"));
 		var data = JSON.parse(window.sessionStorage.getItem("factory_id"));
	//	console.log("data===="+data);
		//(类似于session)
		$("#gw").val(data.gw);
		pageNo = data.pageNo;
 	}
	factory_list(pageRecords,pageNo);
});
/*
(function($){
	$(window).load(function(){
	laundry_product(1);
	find_bigtype();
	})
})(jQuery);
*/
//工位
function find_gwtype(){
	$.ajax({
		url: "../../../../service",
		cache: false,
		async:false,
		data: {
			proc: "g_xmy.find_stationAll",
			para: "['" + username + "','" + secretkey +"']"
		},
		dataType: "json",
		success: function(data){
			var select1 = document.getElementById("gw");
			var op1 = "<option selected='selected' value='-1'>选择类型</option>";
			for(var a=0;a<=data.length-1;a++){
				var xiyitype = data[a].ID;
				op1 = op1+"<option value='"+xiyitype+"'>"+data[a].NAME+"</option>"
			}
			select1.innerHTML = op1;
		}
	});
}

//搜索查询用户
function search(){
	isjump=0;
	factory_list(pageRecords,pageNo);
}

function factory_list(pageRecords,pageNo){
	var gw_type = -1;	//工位类型（仅一种：厂内岗位）
	var gw = document.getElementById("gw").value;
	var gw_name = document.getElementById("gw_name").value;
	factory_id='{"gw":"'+gw+'",';
	factory_id+='"pageNo":"'+pageNo+'"}';
	window.sessionStorage.setItem("factory_id",factory_id);
//  console.log("factory_id==="+factory_id);
//  console.log( username + "','" + secretkey + "','" + pageRecords + "','" + pageNo + "','" + gw_type + "','" + gw + "','" + gw_name);
	$.ajax({
		url: "../../../../service",
		cache: false,
		data: {
			proc: "g_xmy.factory_list",
			para:"['" + username + "','" + secretkey + "','" + pageRecords + "','" + pageNo + "','" + gw_type + "','" + gw + "','" + gw_name +"']"
		},
		dataType: "json",
		success: function(data){
	//		console.log(JSON.stringify(data));
			var tab=document.getElementById("tableid1");
			var ro=tab.rows.length;
			for(var b=1;;){
				if(tab.rows.length==1){
					break;
				}
				tab.deleteRow(b);
			}
			if (data[0].TAG == "0" ){
				layer.alert(data[0].MSG);
				top.location.href="../../login.html"
			}else{

				for(var a=0;a<data.length;a++){
					if(a==data.length-1){
						totalRecords = data[a].ID;
						totalPage = data[a].NAME;
						if(isjump ==0){
							page();
							isjump = 1;
						}
						return;
					}
					var tables=document.getElementById("tableid1");
					var ro=tables.insertRow(a+1);
					cell1=ro.insertCell(0);
					cell1.innerHTML=data[a].ID;
					cell1.align="center";
					cell2=ro.insertCell(1);
					cell2.innerHTML=data[a].NAME;
					cell2.align="center";
					cell3=ro.insertCell(2);
					cell3.innerHTML=data[a].NICKNAME;
					cell3.align="center";
					cell4=ro.insertCell(3);
					cell4.innerHTML=data[a].TYPE;
					cell4.align="center";
					cell5=ro.insertCell(4);
					cell5.innerHTML=data[a].WORK_NAME;
					cell5.align="center";
					cell6=ro.insertCell(5);
					cell6.innerHTML=data[a].LOGIN_TIMES;
					cell6.align="center";
					cell7=ro.insertCell(6);
					cell7.innerHTML=data[a].CREATE_DATE;
					cell7.align="center";
					/*cell8=ro.insertCell(7);
					cell8.innerHTML=data[a].USE_END_TIME;
					cell8.align="center";*/
					cell8=ro.insertCell(7);
					cell8.innerHTML=data[a].LOGIN_LAST_TIME;
					cell8.align="center";
					cell9=ro.insertCell(8);
					cell9.innerHTML=//'<a href="#" onclick="factory_renew('+data[a].NAME+')">续费</a>&nbsp;&nbsp;'+
					'<a href="#" onclick="up_classification('+data[a].ID+')">编辑</a>&nbsp;&nbsp;<a href="#" onclick="delete_classification('+data[a].ID+')">删除</a>';
					cell9.align="center";
				}
			}
		}
	});
}
//分页
function page(){
	laypage.render({
		 elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
		,count:totalRecords //数据总数，从服务端得到
		,curr: pageNo //获取起始页 location.hash.replace('#!fenye=', '')
        ,hash: 'fenye' //自定义hash值
		,layout: ['count', 'prev', 'page', 'next', 'skip']
		,jump: function(obj, first){
			//首次不执行
			if(!first){
		//	  console.log("当前页==="+obj.curr); //得到当前页，以便向服务端请求对应页的数据。
		//	  console.log("每页显示条数==="+obj.limit); //得到每页显示的条数
			  factory_list(obj.limit,obj.curr);
			}
		  }
	});
}
function factory_renew(names){
	$.ajax({
		url: "../../../../service",
		cache : false,
		data : {
			//proc : "g_xmy.add_staff_management",
			proc : "xmy_zyr.factory_renew",
			para : "['" + username + "','" + secretkey + "','" + names + "']"
		},
		dataType : "json",
		success : function(data) {
			console.log(JSON.stringify(data));
			if (data[0].TAG == "1") {
				scan_code(names,data[0].ORDER_ID);
			} else {
				layer.alert(data[0].MSG);
			}
		}
	});
}
function scan_code(login_name,order_id){//扫码支付
	$.ajax({
		url : "/guanjias/Scancode_Pay",
		cache : false,
		data : {
			"money":0.01,
			"state":6,//续费工厂员工
			"h_info":login_name,
			"order_id":order_id,
		},
		dataType : "json",
		success : function(data) {
			console.log(JSON.stringify(data));
			$(".scan_info").attr("src",data[0].URL);
			$(".popup").show();
			//用户是否支付
			countdown=60;
			setInterval('scancode_countdown()',1000);
		}
	});
}

function scancode_countdown() {
	if (countdown == 1) {
		countdown=60;
		popup_hide();
		return false;
	} else {
		$("#times").html(countdown+"s");
		countdown--;
		user_ispay(order_id);
	}

}
function popup_hide(){
	$(".popup").hide();
	top.layer.alert("工厂续费失败!",function(index){
		countdown=60;
		layer.close(index);

	});
	top.layer.closeAll();
}
function user_ispay(order_id){//查询用户是否支付
	$.ajax({
		url: "../../../../service",
		cache: false,
		data: {
			proc: "xmy_zyr.user_ispay",
			para:"['" + order_id + "']"
		},
		dataType: "json",
		success: function(data){
			console.log(JSON.stringify(data));
			if(data[0].A_COUNT=='0'){
				if(countdown==1){
					popup_hide();
				}
			}else{
				top.layer.msg('工厂续费成功!');
				window.location.href="./factory_list.html";
			}
		}
	});
}
//点击查看跳转页面
function up_classification(id){
	$.session.set("staff_id",id);
	window.location.href="./up_factory.html";
}

//删除
function delete_classification(id){
	var username = $.session.get("username");
	var secretkey = $.session.get("user_secretkey");
	layer.confirm('确定删除该员工吗？', {
		  title:'系统提示',
		  btn: ['确定','取消']
		}, function(){
			 $.ajax({
				url: "../../../../service",
				data : {
					proc : "g_xmy.delete_staff",
					para : "['" + username + "','" + secretkey + "','" + id + "']"
				},
				dataType : "json",
				success : function(data) {
					if (data[0].TAG == "1") {
						layer.alert("成功删除该员工!",function(index){
							 window.location.href="./factory_list.html";
							 layer.close(index);
						});
					}else{
						layer.alert("删除失败!");
					//	window.location.href="./error.html";
					}
				}
			});
		});

}
